package ecweb.service.impl;

import ecweb.dal.DatabaseSession;
import ecweb.dao.IAdminUserDao;
import ecweb.dao.impl.AdminUserDao;
import ecweb.domain.AdminUser;
import ecweb.exception.AuthenticationFailedException;
import ecweb.exception.DatabaseException;
import ecweb.exception.ItemNotExistsException;
import ecweb.service.IAdminUserService;

public class AdminUserService implements IAdminUserService {
	
	private IAdminUserDao adminUserDao=new AdminUserDao();

	@Override
	public AdminUser getAdminUserById(long id) throws DatabaseException {
		
		try (DatabaseSession dbses = new DatabaseSession()) {
			return adminUserDao.getAdminUserById(dbses, id);
		}
		
	}

	@Override
	public AdminUser login(String username, String password)
			throws ItemNotExistsException, AuthenticationFailedException,
			DatabaseException {
		
		try (DatabaseSession dbses = new DatabaseSession()) {
			AdminUser adminUser=adminUserDao.getAdminUserByUsername(dbses, username);
			
			if(adminUser==null)
				throw new ItemNotExistsException();
			if (password == null || !password.equals(adminUser.getPassword()))
				throw new AuthenticationFailedException();
			
			return adminUser;
		}
	}

}
